<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>

    <!-- 搜索结果-文章列表 -->
    <div>
      <article-item
        v-for="obj in searchResultList"
        :key="obj.art_id"
        :obj="obj"
        :is-show="false"
          @click.native="$router.push(`/article_detail?art_id=${obj.art_id}`)"
      ></article-item>
    </div>
  </div>
</template>

<script>
import { getSearchResultListAPI } from '@/api'
import ArticleItem from '@/components/ArticleItem'
export default {
  name: 'SearchResult',
  components: {
    ArticleItem
  },
  data () {
    return {
      params: {
        page: 1,
        per_page: 10,
        q: '' // 关键词
      },
      searchResultList: [] // 搜索结果列表
    }
  },
  created () {
    // 提取路径上参数
    this.params.q = this.$route.params.keywords
    this.getSearchResultListFn()
  },
  methods: {
    // 请求-搜索结果列表
    async getSearchResultListFn () {
      const res = await getSearchResultListAPI(this.params)
      // console.log(res)
      this.searchResultList = res.data.data.results
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
.nav-bar-icon {
  color: white;
}
</style>
